package qq;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.FilePartSource;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

public class PostToPoco {
	/*一个登录poco图片上传网站控制面板上传面板的备份2*/
	String html = null;
	String Cookie = null;

	String host[] = { "www.poco.cn", "my.poco.cn" };
	String url[] = {
			"http://www.poco.cn/module_common/login/poco_login_act.php",
			"http://my.poco.cn/album/album_upload_t_y.php?_admin_login" };
	String referer[] = { "http://www.poco.cn/",
			"http://my.poco.cn/album/album_uploader.php" };

	public PostToPoco() {
		HttpClient broser = new HttpClient();

		broser.getHttpConnectionManager().getParams()
				.setConnectionTimeout(30000);

		broser.getHostConfiguration().setProxy("10.37.240.29", 8080); // 代理

		for (int j = 0; j <= 1; ++j) {
			post(broser, j, null);
		}
	}

	private void post(HttpClient broser, int j, String location_url) {
		PostMethod post = null;
		if (j == 2) {
			post = new PostMethod(location_url);
			--j;
		} else
			post = new PostMethod(url[j]);

		if (Cookie != null)
			post.setRequestHeader("Cookie", Cookie);
		post.setRequestHeader("Host", host[j]);
		post.setRequestHeader(
				"User-Agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; aff-kingsoft-ciba)");
		post.setRequestHeader(
				"Accept",
				"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		post.setRequestHeader("Accept-Language", "zh-cn");
		post.setRequestHeader("Proxy-Connection", "keep-alive");
		post.setRequestHeader("Referer", referer[j]);
		if (j == 0) {
			post.setRequestHeader("Pragma", "no-cache");
			post.setRequestHeader("Content-type",
					"application/x-www-form-urlencoded");

			NameValuePair act = new NameValuePair("act", "login");
			NameValuePair CookieDate = new NameValuePair("CookieDate", "1");
			NameValuePair dont_remember_login_state = new NameValuePair(
					"dont_remember_login_state", "1");
			NameValuePair locate = new NameValuePair("locate",
					"http://my.poco.cn/manage/");
			NameValuePair login_type = new NameValuePair("login_type", "0");
			NameValuePair pass_word = new NameValuePair("pass_word",
					"199467miao");
			NameValuePair sbmit = new NameValuePair("sbmit", " ¼");
			NameValuePair user_name = new NameValuePair("user_name",
					"125671911@qq.com");
			NameValuePair warm_tag = new NameValuePair("warm_tag", "1");

			NameValuePair[] nvp1 = new NameValuePair[] { act, CookieDate,
					dont_remember_login_state, locate, login_type, pass_word,
					sbmit, user_name, warm_tag };
			post.setRequestBody(nvp1);
		}

		byte[] buff = new byte[1024];
		int state = 0;
		try {
			state = broser.executeMethod(post);

			System.out.println("post.getPath()=" + post.getPath());
			System.out.println("post.getURI()=" + post.getURI());

			for (int i = 0; i < post.getResponseHeaders().length; ++i) {
				System.out.println("post.getResponseHeader(" + i + ")="
						+ post.getResponseHeaders()[i]);
			}

			if (Cookie == null) {
				// Cookie += "__POCO_POPER__=1;poper_29021=1;";//登陆管理中心
				for (int i = 10; i < post.getResponseHeaders().length; ++i) {

					Cookie += post.getResponseHeaders()[i].getValue()
							.replaceAll("(?:expires=|path=)(.|\n)+", "");
				}
			}

			buff = StreamTool.readInputStream(post.getResponseBodyAsStream());

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("state=" + state);

		if (state == HttpStatus.SC_OK) {
			try {
				html = new String(buff, "gb2312");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			System.out.println("输出网页内容" + html);
		} else {
			System.out.println("连接失败" + "state=" + state);
			// System.out.println(post.getResponseHeader("Location").getValue());
			// 获取源码
			// post(broser, 2 ,post.getResponseHeader("Location").getValue());
			// 上传文件
			upload(broser, post.getResponseHeader("Location").getValue());
		}

		post.releaseConnection();

	}

	private void upload(HttpClient broser, String refer) {
		PostMethod post = new PostMethod(
				"http://imgup-s.poco.cn/ultra_upload_service/upload.mypoco_items.php");

		if (Cookie != null)
			post.setRequestHeader("Cookie", Cookie);
		post.setRequestHeader("Host", "imgup-s.poco.cn");
		post.setRequestHeader(
				"User-Agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; aff-kingsoft-ciba)");
		post.setRequestHeader(
				"Accept",
				"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
		post.setRequestHeader("Accept-Language", "zh-cn");
		post.setRequestHeader("Proxy-Connection", "keep-alive");
		post.setRequestHeader("Referer", refer);
		post.setRequestHeader("Pragma", "no-cache");
		post.setRequestHeader("Content-type",
				"multipart/form-data; boundary=---------------------------7dc36514100a4c");

		File f = null;
		FilePart opus = null;
		try {
			f = new File("D:\\img.jpg");
			opus = new FilePart("opus", new FilePartSource(f.getName(), f));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		post.setRequestHeader("Content-Length", String.valueOf(f.length()));
		StringPart description = new StringPart("description", "");
		StringPart is_add_water = new StringPart("is_add_water", "1");
		StringPart item_type = new StringPart("item_type", "album");
		StringPart set_name = new StringPart("set_name", "");
		StringPart tag_name = new StringPart("tag_name", "");
		StringPart title = new StringPart("title", "");
		StringPart water_type = new StringPart("water_type", "poco");
		StringPart redirect_url = new StringPart(
				"redirect_url",
				"http://my.poco.cn/album/album_items_edit_t_y.php?redirect_url=&op=edit&item_ids[]=%item_id%");

		MultipartRequestEntity mrp = new MultipartRequestEntity(new Part[] {
				description, is_add_water, item_type, opus, redirect_url,
				set_name, tag_name, title, water_type }, post.getParams());

		post.setRequestEntity(mrp);

		byte[] buff = new byte[1024];
		int state = 0;
		try {
			state = broser.executeMethod(post);

			System.out.println("post.getPath()=" + post.getPath());
			System.out.println("post.getURI()=" + post.getURI());

			for (int i = 0; i < post.getResponseHeaders().length; ++i) {
				System.out.println(post.getResponseHeaders()[i]);
			}

			buff = StreamTool.readInputStream(post.getResponseBodyAsStream());

		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("state=" + state);

		if (state == HttpStatus.SC_OK) {
			try {
				html = new String(buff, "gb2312");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			System.out.println("输出网页内容" + html);
		} else {
			System.out.println("连接失败" + "state=" + state);
			// System.out.println(post.getResponseHeader("Location").getValue());
			// 获取源码
			// post(broser, 2 ,post.getResponseHeader("Location").getValue());
		}

		post.releaseConnection();

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new PostToPoco();

		/*
		 * NameValuePair description = new NameValuePair("description", "");
		 * NameValuePair CookieDate = new NameValuePair("CookieDate", "1");
		 * NameValuePair item_type = new NameValuePair("item_type", "album");
		 * NameValuePair set_name = new NameValuePair("set_name", "");
		 * NameValuePair tag_name = new NameValuePair("tag_name", "");
		 * NameValuePair title = new NameValuePair("title", ""); NameValuePair
		 * water_type = new NameValuePair("water_type", "");
		 * 
		 * NameValuePair[] nvp1 = new NameValuePair[] { description, CookieDate,
		 * item_type, set_name, tag_name, title, water_type };
		 * 
		 * post.setRequestBody(nvp1);
		 */

	}

}
